
import Taro from '@tarojs/taro'

// const requestBase="https://api.test.toutiao.applet.rockysaas.com";
const requestBase="https://api-test.applet.rockysaas.com";


// const requestBase = "http://127.0.0.1:3001"
export function get(url, params) {
  if(url.indexOf('//')=== -1){
    url = requestBase + url
  }
  return new Promise((resolve, reject) => {
    Taro.showLoading({
      title: '数据加载中'
    })
    Taro.request({
      url: url,
      data: params,
      method: 'GET',
      header: {
        'content-type': 'application/json;charset=UTF-8'
      },
      success: function (response) {
        // response.setHeader('Content-Type', 'pplication/json; charset=utf-8');
        Taro.hideLoading();
        resolve(response);
      },
      error: function () {
        Taro.hideLoading();
        reject('网络出错');
      }
    })
  });
}

export function post(url, data) {
  if(url.indexOf('//')=== -1){
    url = requestBase + url
  }
  Taro.showLoading({
    title: '数据加载中'
  })
  return new Promise((resolve, reject) => {
    var postData = data;
    Taro.request({
      url: url,
      data: postData,
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
    }).then((response) => {
      Taro.hideLoading();
      resolve(response.data);
    }).catch((err) => {
      Taro.hideLoading();
      reject(err);
    });
  }); 
}
